<?php
/**
 * Created by PhpStorm.
 * User: zy-l 
 * Date: 2019/6/19
 * Time: 1:23 AM
 */

namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use App\Model\AuthRule;
use App\Model\Menu;
use EasySwoole\Validate\Validate;

class Index extends AdminBase
{

    /**
     * 菜单
     * @return Menu
     */
    public function getMenuModel()
    {
        return new Menu();
    }

    public function index()
    {
        $this->assign(["username" => "admin"]);//操作者
        $this->assign(['menu'=>$this->getMenus()]);   //获取菜单列表
        return $this->fetch('index');
    }

    public function getMenus()
    {
        $conf = \EasySwoole\EasySwoole\Config::getInstance()->getConf('AUTH_CONFIG');

        $where=array();
        $where['pid'] = 0;
        $where['hide'] = 0;
        if (!$conf['DEVELOP_MODE']) { // 是否开发者模式
            $where['is_dev'] = 0;
        }
        $menus['main'] = $this->getMenuModel()->getMenuList($where);   //先获取顶级
        foreach ($menus['main'] as $keys => $item) {
            $where = array();
            $where['pid'] = $item['id'];
            $where['hide'] = 0;
            if (!$conf['DEVELOP_MODE']) { // 是否开发者模式
                $where['is_dev'] = 0;
            }
            if (stripos($item['url'], $this->getModule()) !== 0) {
                $item['url'] = $this->getModule() . '/' . $item['url'];
            }
            //判断主菜单权限
            if (!$this->is_root && !$this->checkRule($item['url'], AuthRule::RULE_MAIN, null)) {
                unset($menus['main'][$keys]);
                continue; //继续循环
            }

            $second_urls = $this->getMenuModel()->getMenuList($where);  //获取二级菜单
            foreach ($second_urls as $k => $v) {
                $second_url = $this->getModule() . '/' . $v["url"];
                if (!$this->is_root && !$this->checkRule($second_url, AuthRule::RULE_URL, null)) {

                } else {
                    $g = $v['group'];
                    $menus['main'][$keys]['child'][$g][] = $v;
                }
            }
        }
        return $menus;
    }

    /**
     * 控制台
     * @return string|void
     */
    public function console(){
        return $this->fetch('console');
    }

    /**
     * 数据提交验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;

        return $validate;
    }
}